﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using GalaSoft.MvvmLight;
using MessageWorkflow;
using GalaSoft.MvvmLight.Command;
using MessageWorkflow.Message;

namespace SimpleWpfExample.ViewModel
{
    public class BookViewModel : ViewModelBase
    {
        public const string BookBoughtPropertyName = "BookBought";

        private bool _isBookBought = false;

        public string Title { get; set; }

        public string Description { get; set; }

        public double Price { get; set; }

        public bool BookBought
        {
            get
            {
                return _isBookBought;
            }

            set
            {
                if (_isBookBought != value)
                {
                    _isBookBought = value;

                    RaisePropertyChanged(BookBoughtPropertyName);
                }
            }
        }

        public RelayCommand BuyCommand { get; protected set; }
        public RelayCommand SellCommand { get; protected set; }


        public BookViewModel()
        {
            BuyCommand = new RelayCommand(BuyBook);
            SellCommand = new RelayCommand(SellBook);

            WorkflowChannel.Default.DispatchAsync(new Message.RequestAuthenticatedUser(),
                request =>
                {
                    if (request.HasResult)
                    {
                        UserViewModel userViewModel = request.Result;
                        
                        BookBought = userViewModel.Books.Contains(this);
                    }
                });
        }

        protected void BuyBook()
        {
            WorkflowChannel.Default.DispatchAsync(new Message.RequestAuthenticatedUser(),
                request =>
                {
                    if (request.HasResult)
                    {
                        UserViewModel userViewModel = request.Result;
                        
                        userViewModel.Books.Add(this);
                        BookBought = true;
                    }
                    else 
                    {
                        WorkflowChannel.Default.Dispatch(new ErrorNotificationMessage("Could not buy book since there was no autenticated user found"));
                    }
                });
        }

        protected void SellBook()
        {           

            WorkflowChannel.Default.DispatchAsync(new Message.RequestAuthenticatedUser(),
                request =>
                {
                    if (request.HasResult)
                    {
                        UserViewModel userViewModel = request.Result;

                        userViewModel.Books.Remove(this);
                        BookBought = false;
                    }
                    else 
                    {
                        WorkflowChannel.Default.Dispatch(new ErrorNotificationMessage("Could not sell book since there was no autenticated user found"));
                    }
                });
        }
    }
}
